/** */
package com.smc.common.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zouqinghua
 * @date 2016年9月7日 下午8:58:01
 */
public class PreconditionUtils {

  private static final Logger logger = LoggerFactory.getLogger(PreconditionUtils.class);

  public static void checkArgument(boolean expression) {
    checkArgument(expression, "");
  }

  public static void checkNotFound(boolean expression) {
    checkNotFound(expression, "");
  }

  public static void checkBusiness(boolean expression) {
    checkBusiness(expression, "");
  }

  public static void checkArgument(boolean expression, Object errorMessage) {
    if (!expression) {
      String message = errorMessage == null ? "" : String.valueOf(errorMessage);
      logger.error("参数有错 : {}", message);
      throw new BadRequestException(message);
    }
  }

  public static void checkNotFound(boolean expression, Object errorMessage) {
    if (!expression) {
      String message = errorMessage == null ? "" : String.valueOf(errorMessage);
      logger.error("对象未找查找到 : {}", message);
      throw new NotFoundException(message);
    }
  }

  public static void checkBusiness(boolean expression, Object errorMessage) {
    if (!expression) {
      String message = errorMessage == null ? "" : String.valueOf(errorMessage);
      logger.error("业务异常 : {}", message);
      throw new ServiceException(message);
    }
  }
}
